package personnel.department.pages.widgets;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

public class DateWidget extends AbstractWidget<Date> {

	private String title;
	private Composite parent;
	private CalendarCombo date;

	public DateWidget(Composite parent,  String title, Field field) {
		super(field);
		this.parent = parent;
		this.title = title;
		create();
	}

	@Override
	protected void create() {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title);
		date = new CalendarCombo(parent, SWT.FLAT | SWT.BORDER);
		date.setLayoutData(new GridData(160, 20));
		date.addListener(SWT.Selection, event -> modify());
		super.create();
	}
	
	@Override
	public void setValue(Date value) {
		date.setDate(value);
		modify();
	}
	
	@Override
	public Date getValue() {
		Calendar calendar = date.getDate();
		return calendar == null ? null : personnel.department.utils.DateUtils.extractDate(calendar.getTime());
	}

	@Override
	protected Control getWidget() {
		return date;
	}

}
